<template>
  <div>
    <router-link to="/role/add">
      <el-button type="danger">添加</el-button>
    </router-link>
    <!-- 全部打开：default-expand-all -->
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="rolename" label="角色名称"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1" type="success" size="medium">已启用</el-tag>
          <el-tag v-else type="danger" size="medium">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="edit(scope.row.id)"
          ></el-button>
          <el-button
            type="warning"
            icon="el-icon-view"
            circle
            size="mini"
            @click="view(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="角色信息" :visible.sync="dialogVisible" width="45%">
      <div class="box"><h4>角色名称:&ensp;</h4><span :v-model="roleList.rolename">{{roleList.rolename}}</span></div>
      <h4>权限节点:&ensp;</h4>
      <el-tree
        :data="menulist"
        show-checkbox
        default-expand-all
        node-key="id"
        ref="tree"
        highlight-current
        :props="defaultProps"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger" size="medium" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template><script>
export default {
  data() {
    return {
      tableData: [],
      menulist: [],
      roleList: [],
      defaultProps: {
        children: "children",
        label: "title"
      },
      dialogVisible: false
    };
  },
  created() {
    this.getData();
    // 获取树形控件的内容
    this.getMenuData();
  },
  methods: {
    getData() {
      this.$http.get("/rolelist").then(res => {
        this.tableData = res.data.list;
      });
    },
    // 获取树形控件的数据
    getMenuData() {
      this.$http.get("/menulist", { params: { istree: 1 } }).then(res => {
        this.menulist = res.data.list;
      });
    },
    getDataById(id) {
      this.$http.get("/roleinfo", { params: { id } }).then(res => {
        this.roleList = res.data.list;
        console.log(this.roleList.rolename);

        // this.$refs.tree.setCheckedNodes([{id: 12,}]);
        this.$refs.tree.setCheckedKeys(res.data.list["menus"].split(","));
      });
    },
    edit(id) {
      this.$router.push("/role/" + id);
    },
    del(id) {
      this.$confirm("确认删除吗,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "el-button--danger"
      })
        .then(res => {
          this.doDel(id);
        })
        .catch(_ => {});
    },
    doDel(id) {
      this.$http.post("/menudelete", { id }).then(res => {
        this.tableData = res.data.list;
      });
    },
    view(id) {
      this.dialogVisible = true;
      this.getDataById(id);
    }
  }
};
</script>
<style lang="" scoped>
.el-button {
  margin-bottom: 10px;
}
.box{
  margin-bottom: 10px;
}
h4 {
  padding-left: 100px;
  display: inline-block;
}
.el-tree {
  margin-left: 160px;
  margin-top: -20px;
}
</style>